import VueRouter from "vue-router";
import MyAbout from '../pages/About.vue'
import MyHome from '../pages/Hemo.vue'
import MyMessage from '../pages/Message.vue'
import MyNews from '../pages/News.vue'
import MyDetail from '../pages/Detail.vue'
import MyNewsDetail from "../pages/NewsDetail.vue"

const router = new VueRouter({
    routes: [
        {
            name: 'guanyu',
            path: '/about',
            component: MyAbout,
            meta: {
                title: '关于'
            }
        },
        {
            name: "shouye",
            path: '/home',
            component: MyHome,
            meta: {
                title: '首页'
            },
            children: [
                {
                    name: "xiaoxi",
                    path: 'message',
                    component: MyMessage,
                    meta: {
                        isAuth: true,
                        title: '消息'
                    },

                    children: [
                        {
                            name: 'detail',
                            path: 'detail/:id/:content',
                            component: MyDetail,
                            meta: {
                                title: '消息详情'
                            }
                        }
                    ]
                },
                {
                    name: "xinwen",
                    path: 'news',
                    component: MyNews,
                    meta: {
                        isAuth: true,
                        title: '新闻'

                    },
                    beforeEnter(to, from, next){
                        const token1 = localStorage.getItem("token1");
                        if (to.meta.isAuth) {
                            if (token1 === '22222') {
                                next();
                            } else {
                                alert("暂无权限查看")
                            }
                        } else {
                            next();
                        }
                    },

                    children: [
                        {
                            name: 'test',
                            path: 'newsDetail',
                            component: MyNewsDetail,
                            meta: {
                                title: '新闻详情'
                            }
                        }
                    ]
                }
            ]
        }

    ]
})
export default router;
router.beforeEach((to, from, next) => {
    const token = localStorage.getItem("token");
    if (to.meta.isAuth) {
        if (token === '11111') {
            next();
        } else {
            alert("暂无权限查看")
        }
    } else {
        next();
    }
});
router.afterEach((to) => {
    if(to.meta.title){
        document.title = to.meta.title || 'Vue-Study'
    }else{
        document.title = 'Vue-Study'
    }
})